/*
 * Description: log类
 */

#include "log.h"

#include <iostream>
#include "spdlog/sinks/rotating_file_sink.h"

namespace {
    constexpr size_t MAX_SIZE = 1024 * 1024 * 5;    // 每个日志文件大小
    constexpr size_t MAX_FILES = 1;                 // 最多日志文件个数
}
namespace COMMON {

void Log::Init(std::string logName, std::string logPath)
{
    try {
        auto fileLogger = spdlog::rotating_logger_mt(logName, logPath, MAX_SIZE, MAX_FILES);
        fileLogger->flush_on(spdlog::level::trace);
#ifdef _DEBUG
        fileLogger->set_level(spdlog::level::trace);
#else
        fileLogger->set_level(spdlog::level::info);
#endif
        const std::string pattern = "[%Y-%m-%d %H:%M:%S.%e][%n][%L][%P][%t][%s:%#]%v";
        spdlog::set_pattern(pattern);
        spdlog::set_default_logger(fileLogger);
    } catch (const spdlog::spdlog_ex &ex) {
        std::cout << "Log init failed: " << ex.what() << std::endl;
    }
}
}
